#!/bin/bash
#
# The script should be invoked from the "students" directory. 
# It processes submitted assignments and creates logfile 
#
# SVN specific:
#---------------
# $Author: simonbaev@gmail.com $
# $LastChangedDate: 2011-11-02 09:53:44 -0400 (Wed, 02 Nov 2011) $
# $Revision: 52 $
# 

if [ $# -lt 1 ]; then 
	echo "Usage: $(basename $0) <Assignment ID>"
	echo "Where <Assignment ID> is a subdirectory with the assignment files to be processed for each student."
	exit 0
fi

if [ $# -ge 1 ] && [ "$1" = "--help" ]; then
	echo "TODO: Help message"
	exit 0
fi

# Initialization
#----------------
[ -n "$cis1000path" ] && eval cd "$cis1000path" 2> /dev/null
home="$(pwd)"
if [ ! -d "$home/Students/LF" ] || [ ! -d "$home/Students/FL" ]; then
	echo "Either of the following resulted in the error:"
	echo '1. Environmental variable $cis1000path is not set and current direcory is incorrect'
	echo '2. Variable $cis1000path is set but points to incorrect location'
	echo
	echo 'To resolve the situation cd to the course directory (should contain both "Students/LF" and "Students/FL") or' 
	echo 'create and export $cis1000path to point out to it. Then re-run the script.'
	exit 1
fi
aname="$1"
adir="$home/$aname"
storage="$home/Students/LF"
mkdir -p "$adir/LF"
mkdir -p "$adir/FL"
alog="$adir/$aname".log
touch "$alog".+ "$alog".-
cnt=0
maxlen=25
spaces=$(python -c "print ' '*$maxlen")

# Processing routine
#--------------------
function process {
	if [ $(ls -lp "$1/$aname"| wc -l) -le 1 ]; then
		# no files in the assignment directory => report student name
		echo "-$1" >> "$alog".-	
	else
		# alias for <first name> <last name> as alternative place of storing files
		lf_name=$1
		fl_name=$(echo "$lf_name" | awk '{print $2,$1}')
		# submission files detected
		echo "+$lf_name" >> "$alog".+
		[ -L "$adir/LF/$lf_name" ] || ln -s "$(pwd)/$1/$aname" "$adir/LF/$lf_name"
		[ -L "$adir/FL/$fl_name" ] || ln -s "$(pwd)/$1/$aname" "$adir/FL/$fl_name"
		#[ -L "$adir/LF/$lf_name" ] || {cd "$adir/LF"; ln -s "../../Students/LF/$lf_name/$anamei"}
		#[ -L "$adir/FL/$fl_name" ] || ln -s "$(pwd)/$1/$aname" "$adir/FL/$fl_name"
		cd "$1/$aname"
		tempdir=$$
		mkdir -p "$tempdir"
		cp *.zip "$tempdir/" 2> /dev/null
		cd "$tempdir"
		while true
		do
			zipcnt=0
			for zipfile in *.zip
			do	
				if [ -f "$zipfile" ]; then
					unzip -ujq "$zipfile"
					rm -rf "$zipfile"
					((zipcnt++))
				fi
			done
			[ $zipcnt -eq 0 ] && break
		done
		cp * ../ 2> /dev/null
		cd ..
		tempstr=${spaces:${#1}}
		printf "%s:%s%2d file(s) unzipped\n" "$lf_name" "$tempstr" $(ls -p "$tempdir" | wc -l)
		rm -rf "$tempdir"
		((cnt++))
		cd ../..
	fi
}

# Main part
#-----------
cd "$storage"
if [ -f "$alog" ]; then
	# Check students marked as "didn't submit the work" in $alog file 
	#-----------------------------------------------------------------
	while read line
	do
		[ -z "$line" ] && break
		case "$line" in
		+*)	echo "$line" >> "$alog".+;;
		-*)	stud=$(echo "$line" | sed 's/-//g');
				process "$stud";;
		*) 	echo "Bad entry in log file: $stud" 
		esac		
	done < "$alog"
else
	# Fresh processing (previous processing log file doesn't exist)
	#---------------------------------------------------------------
	for stud in *; do
		if [ -d "$stud" ]; then
			if [ -d "$stud/$aname" ]; then
				process "$stud"
			else
				mkdir -p "$stud/$aname"
			fi
		fi
	done
fi
cd "$home"
# Finalization
#--------------
if [ $cnt -gt 0 ]; then
	printf "\nThere are $cnt new $aname\'s submissions.\n"
	cat "$alog".- "$alog".+ > $alog
else
	echo "No new submissions of '$aname' have been detected."
fi
echo "Total: $(cat "$alog".+ | wc -l) submissions."
rm -rf "$alog".[-+]

